#include <stdio.h>
	using namespace std;
	 
	class Rational
	{
	   public:
	 
	      Rational(int n = 0, int d = 1)
	      {
	         numerator = n;
	         denominator = d;
	      }
	 
	      void print()
	      {
	         cout << numerator << "/" << denominator << endl;
	      }
	 
	      void operator = (Rational c)
	      {
	         numerator = c.numerator;
	         denominator = c.denominator;
	      }
	 
	      void Rational::simplify() // to simplify the fraction
	   {
	         int n=(int)this->numerator;
	         int d=(int)this->denominator;
	         int i=2;
	 
	        while((i<=n) && (n!=1))
	       {
	           while((n/i==(int)n/i) && (d/i ==(int)d/i))
	           {
	              n/=i;
	              d/=i;
	           }
	                 i++;
	       }
	          numerator=n;
	          denominator=(n==0)?1:d;
	   }
	 
	      Rational operator + (Rational c) // adds fractions
	      {
	         Rational::simplify();
	         Rational t;
	         t.numerator = numerator + c.numerator;
	         t.denominator = denominator + c.denominator;
	         return t;
	      }
	 
	      Rational operator - (Rational c) // subtracts fractions
	      {
	         Rational::simplify();
	         Rational t;
	         t.numerator = numerator - c.numerator;
	         t.denominator = denominator - c.denominator;
	         return t;
	      }
	 
	         Rational operator * (Rational c) // multiplies fractions
	        {
	            Rational::simplify();
	            Rational t;
	            t.numerator = numerator * c.numerator;
	            t.denominator = denominator * c.denominator;
	            return t;
	        }
	 
	        Rational operator / (Rational c) // divides fractions
	        {
	            Rational::simplify();
	            Rational t;
	            t.numerator = numerator / c.numerator;
	            t.denominator = denominator / c.denominator;
	            return t;
	         }
	       
	private:
	 
	      int numerator;
	      int denominator;
	 
	}; // end class Rational


int main()
	{
	   Rational a(1,3),b(2,5); // a is (1/3); b is (2/5) // a reduces, b does not
	   Rational z; // test variable to check default constructor
	   Rational c; // instantiates variable c to class Rational initializing to zero
	   c = a + b; // currently adds two numerators (1 + 2) and two denominators (3 + 5)
	   Rational d; // instantiates d
	   d = a - b; // subtracts a from b
	   Rational e; //
	   e = a*b;
	   Rational f;
	   f = a/b;
	   cout << "First fraction is: " << endl;
	   a.print();
	   cout << "\nSecond fraction is: " << endl;
	   b.print();
	   cout << "\nAdding first fraction to second fraction: " << endl;
	   c.print();
	   cout << "\nSubracting first fraction from second fraction: " << endl;
	   d.print();
	   cout << "\nMultiplying first fraction to second fraction: " << endl;
	   e.print();
	   cout << "\nDividing first fraction by second fraction: " << endl;
	   f.print();
	   cout << "\nTest default fraction: " << endl;
	   z.print();
	 
	return 0;
	 
	}
